using System.Collections.Generic;
using System.Linq;

namespace WenYu.Core.CodeGen;

/// <summary>
/// 标准库函数信息 - 用于符号表注册
/// </summary>
public class StandardLibraryFunctionInfo
{
    /// <summary>
    /// 函数名称
    /// </summary>
    public string Name { get; set; } = "";
    
    /// <summary>
    /// 参数类型列表（"任意" 表示接受任何类型，"任意..." 表示可变参数）
    /// </summary>
    public List<string> ParameterTypes { get; set; } = new();
    
    /// <summary>
    /// 返回类型（null表示无返回值）
    /// </summary>
    public string? ReturnType { get; set; }
    
    /// <summary>
    /// 是否为可变参数函数
    /// </summary>
    public bool IsVariadic { get; set; }
    
    /// <summary>
    /// 函数分类（输入输出、数学、字符串等）
    /// </summary>
    public string Category { get; set; } = "通用";
    
    /// <summary>
    /// 函数描述（用于IDE提示和文档）
    /// </summary>
    public string Description { get; set; } = "";
    
    /// <summary>
    /// 使用示例（用于IDE提示）
    /// </summary>
    public string Example { get; set; } = "";
    
    /// <summary>
    /// 相关文档URL
    /// </summary>
    public string? DocumentationUrl { get; set; }
    
    /// <summary>
    /// 是否标准库函数（始终为true）
    /// </summary>
    public bool IsStandardLibrary => true;
    
    /// <summary>
    /// 创建标准库函数信息
    /// </summary>
    public static StandardLibraryFunctionInfo Create(
        string name,
        string returnType,
        params string[] paramTypes)
    {
        return new StandardLibraryFunctionInfo
        {
            Name = name,
            ReturnType = returnType,
            ParameterTypes = paramTypes.ToList(),
            IsVariadic = paramTypes.Any(p => p.EndsWith("..."))
        };
    }
    
    /// <summary>
    /// 创建可变参数函数
    /// </summary>
    public static StandardLibraryFunctionInfo CreateVariadic(
        string name,
        string returnType,
        string variadicType = "任意")
    {
        return new StandardLibraryFunctionInfo
        {
            Name = name,
            ReturnType = returnType,
            ParameterTypes = new List<string> { $"{variadicType}..." },
            IsVariadic = true
        };
    }
    
    /// <summary>
    /// 添加描述
    /// </summary>
    public StandardLibraryFunctionInfo WithDescription(string description)
    {
        Description = description;
        return this;
    }
    
    /// <summary>
    /// 添加示例
    /// </summary>
    public StandardLibraryFunctionInfo WithExample(string example)
    {
        Example = example;
        return this;
    }
    
    /// <summary>
    /// 添加分类
    /// </summary>
    public StandardLibraryFunctionInfo WithCategory(string category)
    {
        Category = category;
        return this;
    }
    
    /// <summary>
    /// 添加文档URL
    /// </summary>
    public StandardLibraryFunctionInfo WithDocUrl(string url)
    {
        DocumentationUrl = url;
        return this;
    }
}

